using UnityEngine;

public class DemoHelper : MonoBehaviour
{
    private int mMargin = 10;
    private int mBtnWidth = -1;
    private int mBtnHeight;
    private GUILayoutOption mGuiBtnWidth = null;
    private GUILayoutOption mGuiBtnHeight = null;
    private GUIStyle mTextSytle = null;
    private string mTestResult = "";

    private DemoOHJSClass mDemoClass = new DemoOHJSClass();
    private DemoOHJSObject mDemoObject = new DemoOHJSObject();
    private DemoJSCallback mDemoCallback = new DemoJSCallback();
    private DemoOpenHarmonyJSArrayBuffer mDemoArrayBuffer = new DemoOpenHarmonyJSArrayBuffer();
    private DemoOHETsObject mDemoEtsObject = new DemoOHETsObject();
    private DemoETsCallCSharp mDemoCallCS = new DemoETsCallCSharp();

    private void Start()
    {
        Debug.Log($"width={Screen.width} height={Screen.height}");
    }

    private void Init()
    {
        int screenWith = Screen.width;
        int screenHeight = Screen.height;

        mBtnWidth = screenWith / 2 - 30;
        mBtnHeight = screenHeight / 10 - 10;
        mGuiBtnWidth = GUILayout.Width(mBtnWidth);
        mGuiBtnHeight = GUILayout.Height(mBtnHeight);
        
        mTextSytle = new GUIStyle("Button");
        mTextSytle.fontSize = 38;
        mTextSytle.wordWrap = true;
        mTextSytle.alignment = TextAnchor.UpperLeft;
    }

    private void OnSensor(string msg)
    {
        mTestResult = msg;
    }
    
    private void OnGUI()
    {
        if (mBtnWidth == -1)
        {
            Init();
        }

        int row = 3;
        GUILayout.BeginArea(new Rect(mMargin, mMargin, Screen.width - mMargin * 2, mBtnHeight * row));
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Static",  mTextSytle, mGuiBtnWidth, mGuiBtnHeight))
        {
            mDemoCallback.Stop();
            mTestResult = mDemoClass.Test();
        }
        if (GUILayout.Button("Object", mTextSytle,mGuiBtnWidth, mGuiBtnHeight))
        {
            mDemoCallback.Stop();
            mTestResult = mDemoObject.Test();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("SysCallback", mTextSytle,mGuiBtnWidth, mGuiBtnHeight))
        {
            mDemoCallback.Test(OnSensor);

        }
        if (GUILayout.Button("ArrayBuffer", mTextSytle,mGuiBtnWidth, mGuiBtnHeight))
        {
            mDemoCallback.Stop();
            mTestResult = mDemoArrayBuffer.Test();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("EtsLib", mTextSytle,mGuiBtnWidth, mGuiBtnHeight))
        {
            mDemoCallback.Stop();
            mTestResult = mDemoEtsObject.Test();
        }
        if (GUILayout.Button("LowLevelAPI", mTextSytle,mGuiBtnWidth, mGuiBtnHeight))
        {
            mDemoCallback.Stop();
            mTestResult = mDemoCallCS.Test();
        }
        GUILayout.EndHorizontal();
        GUILayout.EndArea();
        GUILayout.BeginArea(new Rect(mMargin, (mBtnHeight + mMargin) * row, Screen.width - mMargin * 2, Screen.height - (mBtnHeight + mMargin) * row));
        GUILayout.Label(mTestResult, mTextSytle);
        GUILayout.EndArea();
    }
}
